package com.wcs.exam.controller.admin.biz;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.wcs.exam.common.base.BaseBiz;
import com.wcs.exam.common.base.Result;
import com.wcs.exam.common.cache.CacheCount;
import com.wcs.exam.controller.admin.resp.*;
import com.wcs.exam.dao.*;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 注册登录统计
 *
 * @author wcs
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AdminDashboardBiz extends BaseBiz {

    @NotNull
    private final UsersDao usersDao;
    @NotNull
    private final UsersLogDao usersLogDao;
    @NotNull
    private final OrderInfoDao orderInfoDao;
    @NotNull
    private final ExamDao examDao;
    @NotNull
    private final PaperDao paperDao;
    @NotNull
    private final QuestionDao questionDao;

    @NotNull
    private final CacheCount cacheCount;

    public Result<AdminDashboardStatLoginResp> statLogin(Integer dates) {
        AdminDashboardStatLoginResp resp = new AdminDashboardStatLoginResp();
        List<AdminStatLogin> respList = usersLogDao.statByDate(dates);
        if (CollUtil.isNotEmpty(respList)) {
            resp.setDateList(respList.stream().map(AdminStatLogin::getDates).distinct().collect(Collectors.toList()));
            Map<String, Long> loginMap = respList.stream().filter(s -> s.getLoginStatus().equals(1)).collect(Collectors.toMap(AdminStatLogin::getDates, AdminStatLogin::getLogins));
            Map<String, Long> registerMap = respList.stream().filter(s -> s.getLoginStatus().equals(2)).collect(Collectors.toMap(AdminStatLogin::getDates, AdminStatLogin::getLogins));
            List<Long> loginList = new ArrayList<>();
            List<Long> registerList = new ArrayList<>();
            for (String data : resp.getDateList()) {
                loginList.add(loginMap.getOrDefault(data, 0L));
                registerList.add(registerMap.getOrDefault(data, 0L));
            }
            resp.setLoginList(loginList);
            resp.setRegisterList(registerList);
        }
        return Result.success(resp);
    }

    public Result<AdminDashboardStatDataResp> statData() {
        AdminDashboardStatDataResp resp = new AdminDashboardStatDataResp();
        String tomorrow = DateUtil.formatDate(DateUtil.tomorrow());
        String yesterday = DateUtil.formatDate(DateUtil.yesterday());
        List<AdminStatOrderInfo> stats = orderInfoDao.stat(yesterday, tomorrow);
        if (CollUtil.isNotEmpty(stats)) {
            Map<String, AdminStatOrderInfo> maps = stats.stream().collect(Collectors.toMap(AdminStatOrderInfo::getDates, s -> s));
            AdminStatOrderInfo todayMap = maps.get(DateUtil.today());
            if (ObjectUtil.isNotNull(todayMap)) {
                resp.setTodayOrder(todayMap.getOrders());
                resp.setTodayMoney(todayMap.getMoneys());
            }
            AdminStatOrderInfo yesterdayMap = maps.get(yesterday);
            if (ObjectUtil.isNotNull(yesterdayMap)) {
                resp.setYesterdayOrder(yesterdayMap.getOrders());
                resp.setYesterdayMoney(yesterdayMap.getMoneys());
            }
        }

        resp.setUserSum(usersDao.count());
        resp.setExamSum(examDao.count());
        resp.setPaperSum(paperDao.count());
        resp.setQuestionSum(questionDao.count());
        AdminStatOrder statResp = orderInfoDao.stat(null);
        resp.setOrderSum(statResp.getCourseBuySum());
        resp.setOrderMoney(statResp.getCourseBuyMoney());

        resp.setOnlineUser(cacheCount.getOnlineUser());
        resp.setOnlineUserExam(cacheCount.getOnlineUserExam());
        return Result.success(resp);
    }

}
